package com.b2b.util;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.b2b.model.CompanyRoleDto;
import com.b2b.model.CompanyUserRoleDto;

import com.b2b.model.entity.company.CompanyRole;
import com.b2b.model.entity.company.CompanyUserRole;
import com.b2b.model.receive.Staff;
import com.b2b.model.receive.StaffDto;
import com.b2b.service.CompanyRoleService;
import com.b2b.service.CompanyUserRoleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


@Component
public class CompanyRoleUtil {

    private final CompanyRoleService companyRoleService;
    private final CompanyUserRoleService companyUserRoleService;
    private final UserUtil userUtil;

    @Autowired
    public CompanyRoleUtil(CompanyRoleService companyRoleService, CompanyUserRoleService companyUserRoleService, UserUtil userUtil) {
        this.companyRoleService = companyRoleService;
        this.companyUserRoleService = companyUserRoleService;
        this.userUtil = userUtil;
    }



    public Integer insertRole(CompanyRoleDto companyRoleDto) {
        CompanyRole companyRole = getCompanyRole(companyRoleDto);
        CompanyRole companyRoleTemp = new CompanyRole();
        companyRoleTemp.setRoleName(companyRoleDto.getRoleName());
        companyRoleTemp.setCompanyId(companyRoleDto.getCompanyId());
        int count1 = companyRoleService.selectCount(companyRoleTemp);
        if (count1 > 0) {
            count1 = -2;
            return count1;
        } else {
            int count2 = companyRoleService.insert(companyRole);
            if (count2 <= 0) {
                return count2;
            } else {
                return companyRole.getRoleId();
            }
        }

    }

    public List<CompanyRole> getByCompanyId(Integer companyId) {
        CompanyRole companyRole = new CompanyRole();
        companyRole.setCompanyId(companyId);

        return companyRoleService.select(companyRole);
    }

    public Integer updateUserRole(Integer userId, Integer roleId) {
        CompanyUserRoleDto companyUserRole = new CompanyUserRoleDto();
        companyUserRole.setUserId(userId);
        int count = companyUserRoleService.selectCount(companyUserRole);
        if (count > 0) {
            companyUserRole.setRoleId(roleId);
            count = companyUserRoleService.updateByPrimaryKeySelective(companyUserRole);
        }
        return count;
    }

    public Integer deleteUserRole(Integer userId) {
        CompanyUserRoleDto companyUserRole = new CompanyUserRoleDto();
        companyUserRole.setUserId(userId);
        return companyUserRoleService.updateByPrimaryKeySelective(companyUserRole);
    }

    public List<CompanyRole> getAll(Integer companyId) {
        CompanyUserRoleDto companyUserRoleDto = new CompanyUserRoleDto();
        companyUserRoleDto.setCompanyId(companyId);
        List<CompanyUserRoleDto> companyUserRoleDtoList = companyUserRoleService.select(companyUserRoleDto);
        List<CompanyRole> companyRoleList = new ArrayList<>();
        for (CompanyUserRoleDto userRoleDto : companyUserRoleDtoList) {
            CompanyRole companyRole = getByRoleId(userRoleDto.getRoleId());
            companyRoleList.add(companyRole);
        }
        return companyRoleList;
    }

    public CompanyRole getByRoleId(Integer roleId) {
        return companyRoleService.selectByPrimaryKey(roleId);
    }

    public CompanyRoleDto getById(CompanyRoleDto companyRoleDto) {
        CompanyRole companyRole = getCompanyRole(companyRoleDto);
        companyRole = companyRoleService.selectByPrimaryKey(companyRole);

        return getCompanyRoleDto(companyRole);
    }

    public List<Staff> getStaff(StaffDto staffDto) {
        List<Staff> staffList = new ArrayList<>();
        Integer companyId = staffDto.getCompanyId();
        CompanyUserRoleDto companyUserRole = new CompanyUserRoleDto();
        companyUserRole.setCompanyId(companyId);
        List<CompanyUserRoleDto> companyUserRoles = companyUserRoleService.select(companyUserRole);
        for (CompanyUserRoleDto userRole : companyUserRoles) {
            if (userRole.getRoleId() != 0) {
                Staff staff = getStaff(userRole);
                staffList.add(staff);
            }
        }

        return staffList;
    }

    public Staff getStaff(CompanyUserRole companyUserRole) {
        Staff staff = new Staff();
        staff.setRoleId(companyUserRole.getRoleId());
        staff.setUserId(companyUserRole.getUserId());
        staff.setUserName(userUtil.getUserName(companyUserRole.getUserId()));
        return staff;
    }

    public int updateCompanyRole(CompanyRoleDto companyRoleDto) {
        CompanyRole companyRole = new CompanyRole();
        companyRole.setRoleId(companyRoleDto.getRoleId());
        int count = companyRoleService.selectCount(companyRole);
        if (count > 0) {
            companyRole = companyRoleService.selectOne(companyRole);
            CompanyRole companyRoleTemp = new CompanyRole();
            companyRoleTemp.setCompanyId(companyRole.getCompanyId());
            companyRoleTemp.setRoleName(companyRole.getRoleName());
            count = companyRoleService.selectCount(companyRoleTemp);
            if (count > 0){
                count = -3;
            }else {
                count = companyRoleService.updateByPrimaryKeySelective(getCompanyRole(companyRoleDto));
            }
        } else {
            count = -2;
        }
        return count;
    }

    public int deleteCompanyRole(Integer roleId) {
        CompanyRole companyRole = new CompanyRole();
        companyRole.setRoleId(roleId);
        int count = companyRoleService.selectCount(companyRole);
        if (count <= 0) {
            count = 2;
        } else {
            count = companyRoleService.deleteByPrimaryKey(roleId);
        }
        return count;
    }

    public CompanyRole getCompanyRole(CompanyRoleDto companyRoleDto) {
        CompanyRole companyRole = new CompanyRole();

        String roleRouteAuthorityList = JSON.toJSONString(companyRoleDto.getRoleRouteAuthorityList());
        String roleAPIAuthorityList = JSON.toJSONString(companyRoleDto.getRoleAPIAuthorityList());

        companyRole.setCompanyId(companyRoleDto.getCompanyId());
        companyRole.setRoleId(companyRoleDto.getRoleId());
        companyRole.setRoleName(companyRoleDto.getRoleName());
        if (!roleRouteAuthorityList.equals("null")){
            companyRole.setRoleRouteAuthorityList(roleRouteAuthorityList);
        }
        if (!roleAPIAuthorityList.equals("null")){
            companyRole.setRoleApiAuthorityList(roleAPIAuthorityList);
        }

        return companyRole;
    }

    public CompanyRoleDto getCompanyRoleDto(CompanyRole companyRole) {
        CompanyRoleDto companyRoleDto = new CompanyRoleDto();

        List<Integer> roleRouteAuthorityList = JSONObject.parseArray(companyRole.getRoleRouteAuthorityList(), Integer.class);
        List<Integer> roleAPIAuthorityList = JSONObject.parseArray(companyRole.getRoleApiAuthorityList(), Integer.class);

        companyRoleDto.setRoleId(companyRole.getRoleId());
        companyRoleDto.setRoleName(companyRole.getRoleName());
        companyRoleDto.setRoleRouteAuthorityList(roleRouteAuthorityList);
        companyRoleDto.setRoleAPIAuthorityList(roleAPIAuthorityList);

        return companyRoleDto;
    }

    public List<Integer> getRole(Integer roleId) {
        CompanyRole companyRole = companyRoleService.selectByPrimaryKey(roleId);
        return getCompanyRoleDto(companyRole).getRoleRouteAuthorityList();
    }
}
